package services

import (
	"gorm.io/gorm"
	"server/global"
	"server/internal/model"
	"time"
)

var UserGroup = new(userGroup)

type userGroup struct {
	model.UserGroup
}

// GetList 列表
func (a userGroup) GetList(Db *gorm.DB) ([]userGroup, int64) {
	var list []userGroup
	var total int64
	Db.Model(&a).Find(&list).Offset(-1).Count(&total)
	return list, total
}

// GetById 单条
func (a userGroup) GetById(id any) userGroup {
	Db := global.GL_DB
	Db.Model(&a).Where("id =?", id).First(&a)

	return a

}

// Create 新增
func (userGroup) Create(data model.UserGroup) (model.UserGroup, error) {
	Db := global.GL_DB
	t := time.Now()
	data.CreateTime = int(t.Unix())
	data.UpdateTime = int(t.Unix())
	Db.Create(&data)
	return data, Db.Error
}

// Update 编辑
func (userGroup) Update(data model.UserGroup) (model.UserGroup, error) {
	Db := global.GL_DB
	t := time.Now()
	data.UpdateTime = int(t.Unix())
	Db = Db.Updates(&data)
	return data, Db.Error

}

// Delete 删除
func (a userGroup) Delete(ids []int) *gorm.DB {
	Db := global.GL_DB

	return Db.Delete(&a, ids)

}
